"""
TLDRepo

	This class is made to generate a definitive list of TLDs and includes some utilities that modify
	a given url's TLD.

"""	

import re
from time import gmtime, strftime

import urllib2

from threading import Thread, Lock
from Queue import Queue

class OpenDNS():
	location = 'guidetest.a.id.opendns.com'
	server = 'OpenDNS Guide'

class TLDRepo():

	_ianaDatabaseUrl = 'http://www.iana.org/domains/root/db/'
	_wikiDatabaseUrl = 'http://en.wikipedia.org/wiki/List_of_Internet_top-level_domains'
	
	_patIanaTLD = re.compile('<a href="/domains/root/db/[^"]*">(\.[A-Z]+)</a></td>')
	_patWikiTLD = re.compile(r'<td><a href="/wiki/(\.[^"]+)" title="\1">\1</a></td>')
	
	def __init__(self, repo_name='tld303.repo' ):
		self._TLDs = []
		self._length = 0
		self._REPO_NAME=repo_name
		try:
			with open( repo_name, 'r') as x:
				rdate = x.next().split('.') #last repo update
				ryear, rmonth, rday = int(rdate[0]), int(rdate[1]), int(rdate[2])
				cdate = gmtime() #current date
				year,month,day = cdate.tm_year, cdate.tm_mon, cdate.tm_mday
				if year - ryear > 0 or month - rmonth > 0 or day - rday > 6: #if its been a week
					self.GenerateRepo(repo_name)
				else:
					for tld in x:
						self._TLDs.append(tld.strip('.\n'))
		except IOError:	#Couldn't find repo or couldn't open
			self.GenerateRepo(repo_name)
		self._length = len( self._TLDs )
	
	def GenerateRepo(self, repo_name ):	
		wikiOpener = urllib2.build_opener()
		wikiOpener.addheaders = [('User-agent', 'Mozilla/5.0')]
		
		ianaDatabase = urllib2.urlopen( self._ianaDatabaseUrl ).read()
		wikiDatabase = wikiOpener.open( self._wikiDatabaseUrl ).read()
		
		ianaTLDs = set( [ x.lower() for x in re.findall( self._patIanaTLD, ianaDatabase ) ] )
		wikiTLDs = set( re.findall( self._patWikiTLD, wikiDatabase ) )
		
		self._TLDs = sorted( ianaTLDs | wikiTLDs )
		
		with open( repo_name, 'w') as x:
			x.write( strftime("%Y.%m.%d\n",gmtime()) )
			for tld in self._TLDs:
				x.write(tld + '\n' )
				
	def getTLDs(self):
		return self._TLDs
		
	def __len__( self ):
		return self._length

	"""
	TLD Utilities
	"""
	
	def fbTLD(self, url):
		return self.FirstBeforeTLD(url)
		
	def FirstBeforeTLD(self, url):
		urlpcs = url.split('.')
		i = -1
		if ':' in urlpcs[-1]:
			urlpcs.extend(urlpcs.pop().split(':'))
			i = -2
		while (urlpcs[i] in self._TLDs):
			i -= 1
		return i

	def swapTLD(self, url, tld):
		return '.'.join( url.split('.')[:self.fbTLD(url)+1] ) + '.' + tld

	def NSurl(self, url):
		return '.'.join( url.split('.')[self.fbTLD(url):] )

			
